package com.earthquake.monitor_system.controller;

import com.earthquake.monitor_system.service.impl.UploadImp;
import com.earthquake.monitor_system.utils.Result;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/upload")
public class UploadController {
    @Autowired
    private UploadImp uploadImp;
    @PostMapping("/file")
    public Result upload(@RequestParam("file") MultipartFile multipartFile, HttpServletRequest request) throws IOException {
        if(multipartFile.isEmpty()) {
            return Result.error("上传失败！", null);
        }
        String dir = request.getParameter("dir");
        String fileUrl = uploadImp.uploadImg(multipartFile, dir);
        return Result.success("上传成功！",fileUrl);
    }

}
